package com.atguigu.tingshu.user.service.impl;

import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {


	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Override
	public UserInfo getUserInfo() {
		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		//从redis中获取用户信息
		UserInfo userInfo = userInfoMapper.selectById(userId);
		return userInfo;
	}

	/**
	 * 修改用户
	 * @param userInfoVo
	 */
	@Override
	public void updateUser(UserInfoVo userInfoVo) {
		//获取当前登录id
		Long userId = AuthContextHolder.getUserId();
		System.out.println(userId);
		UserInfo userInfo = this.getById(userId);
		BeanUtils.copyProperties(userInfoVo, userInfo);
		userInfo.setId(userId);

		this.updateById(userInfo);
	}

	/**
	 * 根据userId 获取到用户信息
	 * @param userId
	 * @return
	 */
	@Override
	public UserInfoVo getUserInfoVoById(Long userId) {
		UserInfo userInfo = this.lambdaQuery()
				.eq(UserInfo::getId, userId)
				.one();
		UserInfoVo userInfoVo = new UserInfoVo();
		BeanUtils.copyProperties(userInfo,userInfoVo);
		return userInfoVo;
	}
}
